<?php
namespace wlcat\http;
use Think\Template\Driver\Smart;
use wlcat\http\MiddleWare\MiddleFrameWareInterface;
use wlcat\src\Config;
use wlcat\src\Request;
use wlcat\src\Register;
use wlcat\src\templet\SmartyAdapt;

class Core {
    protected $request;
    public function __construct()
    {
        $this->init();

    }
    public function init()
    {
        //自动加载
        spl_autoload_register(array($this, 'loadClass'));
        //config配置
        Config::init();
        //注册容器
        $this->regitster();
        //设置request
        $this->request = Register::get(Register::REQUEST_CLASS);
    }
    public function regitster()
    {
        //注册容器
        require_once 'RegitsterSet.php';
    }

    public function run() {
        $this->beforeMiddleWare();
        //路径
        $this->Route();
    }
    public function beforeMiddleWare()
    {
        $kernels = include_once FRAME_PATH.'http/Kernel.php';
        if (!is_array($kernels))
        {
            return false;
        }
        foreach ($kernels as $kernel)
        {
            if (!empty($kernel))
            {
                if (class_exists($kernel))
                {
                    $middleWare = new $kernel();
                    if (! $middleWare instanceof MiddleFrameWareInterface)
                    {
                        exit('中间件: '.$kernel." 没有实现接口 \\wlcat\\http\\MiddleWare\\MiddleFrameWareInterface");
                    }
                    $middleWare->handle($this->request, function(Request $request) {
                        $this->request = $request;
                        Register::set(Register::REQUEST_CLASS, $request);
                    });
                } else {
                    exit('中间件不存在: '.$kernel);
                }

            }
        }
    }
    public function Route()
    {
        // xxx.aa.com/Index/index/参数
        $defaultMode    = "";
        $controllerName = 'Index';
        $action         = 'index';
        $queryString    = array();
        $url            = $this->request->input('get.url', null);
        if (!empty($url)) {
            $urlArray = explode('/', $url);
            // 获取控制器名
            $controllerName = ucfirst($urlArray[0]);
            // 获取动作名
            array_shift($urlArray);
            $action = empty($urlArray[0]) ? 'index' : $urlArray[0];
            //获取URL参数
            array_shift($urlArray);
            $queryString = empty($urlArray) ? array() : $urlArray;
        }
        $namespace = array('app');
        if (!empty($defaultMode))
        {
            $namespace[] = $defaultMode;
        }
        $namespace[] = "controllers";
        $namespace[] = $controllerName;

        // 实例化控制器
        $controller = implode('\\',$namespace) . 'Controller';
        if (class_exists($controller))
        {
            $dispatch = new $controller($controllerName, $action);
            // 如果控制器存和动作存在，这调用并传入URL参数
            if ((int)method_exists($controller, $action)) {
                call_user_func_array(array($dispatch, $action), $queryString);
            } else {
                exit($controller . "控制器不存在");
            }
        } else {
            exit($controller . "控制器不存在");
        }

    }

//    // 自动加载控制器和模型类
    static function loadClass($class)
    {
        $class_bak = $class;
        $class = explode('\\', $class);
        if ($class[0] == 'app')
        {
            $class[0] = APPLICATION_NAME;
        }
        $file = APP_PATH.implode('/', $class).".php";
        if (file_exists($file))
        {
            require_once $file;
        } else {
//            exit("找不到文件：".$class_bak);
        }


    }
}